
package com.compuvest.web;

// <editor-fold defaultstate="collapsed" desc="import">
import java.io.Serializable;
import java.util.Map;
// </editor-fold>

public class Item implements Serializable {

// <editor-fold defaultstate="collapsed" desc="init">
    // warranty
    private final static int WARRANTY_NONE       = 0;      // 0000 0000
    private final static int WARRANTY_30D        = 0x1;    // 0000 0001
    private final static int WARRANTY_60D        = 0x1<<1; // 0000 0010
    private final static int WARRANTY_90D        = 0x1<<2; // 0000 0100
    private final static int WARRANTY_180D       = 0x1<<3; // 0000 1000
    private final static int WARRANTY_1Y         = 0x1<<4; // 0001 0000
    private final static int WARRANTY_3Y         = 0x1<<5; // 0010 0000
    private final static int WARRANTY_5Y         = 0x1<<6; // 0100 0000
    private final static int WARRANTY_LIFE       = 0x1<<7; // 1000 0000

    private final static int MANUFACTURE_W30D    = 0x1<<8;  // 0000 0001 0000 0000
    private final static int MANUFACTURE_W45D    = 0x1<<9;  // 0000 0010 0000 0000
//    private final static int MANUFACTURE_W90D  = 0x1<<10; // 0000 0100 0000 0000
    private final static int MANUFACTURE_W180D   = 0x1<<11; // 0000 1000 0000 0000
    private final static int MANUFACTURE_W1Y     = 0x1<<12; // 0001 0000 0000 0000
    private final static int MANUFACTURE_W3Y     = 0x1<<13; // 0010 0000 0000 0000
    private final static int MANUFACTURE_W5Y     = 0x1<<14; // 0100 0000 0000 0000

    public final static int EXTENDED_WARRANTY    = 0x00FF; // 0000 0000 1111 1111 mask extended warranty
    public final static int MANUFACTURE_WARRANTY = 0xFF00; // 1111 1111 0000 0000 mask manufacture warranty

    private final static String W30DAYS      = "30 Days";
    private final static String W45DAYS      = "45 Days";
    private final static String W60DAYS      = "60 Days";
    private final static String W90DAYS      = "90 Days";
    private final static String W180DAYS     = "180 Days";
    private final static String W1YEAR       = "1 Year";
    private final static String W3YEARS      = "3 Years";
    private final static String W5YEARS      = "5 Years";
    private final static String WLIFETIME    = "Life Time";

    private int itemID;
    private int itemIDR;
    private int categoryID;
    private int vendorID;
    private String itemName = "";
    private String accwareCode = "";
    private String upc;
    private String categoryName = "";
    private String subcatName = "";
    private int categoryParent;
    private String description = "";
    private String descrJSON = "";
    private double price;
    private double costPrice;
    private double priceR;

    private double promoProcent = 0d;
    private String promoCode = "";

    private double resPrice1;
    private double resPrice2;
    private double resPrice3;
    private int resQuantity2;
    private int resQuantity3;
    private int quantity;
    private int quantityR;
    private boolean stock = false;
    private boolean stockR = false;
    private String status = "";
    private double weight;
    private String mfrName = "";
    private String mfrNumber = "";
    private String mfrLink;
    private int conditionid = 0;
    private String condition = "";
    private String packages;
    private String packagesInclude;
    private String comments = "";
    private int bannerMask;
    private int warrantyMask;
    private double warranty1;
    private double warranty2;
    private double warranty3;

    // second items (relative)
    private double relativePrice;
    private boolean relativeStock = false;
    private int relativeQuantity;

    private Map<String,Double> promocode;
// </editor-fold>

    public Item() {
        this(0, 0, "", 0d, "", 0);
    }

    public Item(int itemID, int categoryID, String itemName, double sellPrice, String accwareCode, int categoryParentID) {
        this.itemID = itemID;
        this.categoryID = categoryID;
        this.itemName = itemName;
        this.price = sellPrice;
        this.accwareCode = accwareCode;
        this.categoryParent = categoryParentID;
    }

// <editor-fold defaultstate="collapsed" desc="[sg]et()">

    public int getItemID()                          { return itemID; }
    public void setItemID(int itemID)               { this.itemID = itemID; }

    public int getItemIDR()                         { return itemIDR; }
    public void setItemIDR(int itemIDR)             { this.itemIDR = itemIDR; }

    public int getCategoryID()                      { return categoryID; }
    public void setCategoryID(int categoryID)       { this.categoryID = categoryID; }

    public int getVendorID()                        { return vendorID; }
    public void setVendorID(int vendorID)           { this.vendorID = vendorID; }

    public String getItemName()                     { return itemName; }
    public void setItemName(String itemName)        { this.itemName = itemName; }

    public String getAccwareCode()                  { return accwareCode; }
    public void setAccwareCode(String accwareCode)  { this.accwareCode = accwareCode; }

    public String getUPC()                          { return upc; }
    public void setUPC(String upc)                  { this.upc = upc; }

    public String getCategoryName()                 { return categoryName; }
    public void setCategoryName(String categoryName){ this.categoryName = categoryName; }

    public int getCategoryParent()                  { return categoryParent; }
    public void setCategoryParent(int parent)       { this.categoryParent = parent; }

    public String getSubcategoryName()              { return subcatName; }
    public void setSubcategoryName(String subcat)   { this.subcatName = subcat; }

    public String getDescription()                  { return description; }
    public void setDescription(String descr)        { this.description = descr; }

    public String getJSONDescription()              { return descrJSON.length() == 0 ? null : descrJSON; }
    public void setJSONDescription(String descr)    { this.descrJSON = descr; }

    public double getPrice()                        { return price; }
    public void setPrice(double price)              { this.price = price; }

    public double getCostPrice()                    { return costPrice; }
    public void setCostPrice(double costPrice)      { this.costPrice = costPrice; }

    public double getPriceR()                       { return priceR; }
    public void setPriceR(double priceR)            { this.priceR = priceR; }

    public double getPromoProcent()                 { return promoProcent; }
    public void setPromoProcent(double promoProcent){ this.promoProcent = promoProcent; }

    public double getResPrice1()                    { return resPrice1; }
    public void setResPrice1(double price1)         { this.resPrice1 = price1; }

    public double getResPrice2()                    { return resPrice2; }
    public void setResPrice2(double price2)         { this.resPrice2 = price2; }

    public double getResPrice3()                    { return resPrice3; }
    public void setResPrice3(double price3)         { this.resPrice3 = price3; }

    public int getResQuantity2()                    { return resQuantity2; }
    public void setResQuantity2(int quantity2)      { this.resQuantity2 = quantity2; }

    public int getResQuantity3()                    { return resQuantity3; }
    public void setResQuantity3(int quantity3)      { this.resQuantity3 = quantity3; }

    public int getQuantity()                        { return quantity; }
    public void setQuantity(int quantity)           { this.quantity = quantity; }

    public int getQuantityR()                       { return quantityR; }
    public void setQuantityR(int quantityR)         { this.quantityR = quantityR; }

    public boolean getStock()                       { return stock; }
    public void setStock(boolean stock)             { this.stock = stock; }

    public boolean getStockR()                      { return stockR; }
    public void setStockR(boolean stockR)           { this.stockR = stockR; }

    public String getStatus()                       { return status; }
    public void setStatus(String status)            { this.status = status; }

    public double getWeight()                       { return (weight == 0d) ? 0.1 : weight; }
    public void setWeight(double weight)            { this.weight = weight; }

    public String getMfrName()                      { return mfrName; }
    public void setMfrName(String mfrName)          { this.mfrName = mfrName; }

    public String getMfrNumber()                    { return mfrNumber; }
    public void setMfrNumber(String mfrNumber)      { this.mfrNumber = mfrNumber; }

    public String getMfrLink()                      { return mfrLink; }
    public void setMfrLink(String mfrLink)          { this.mfrLink = mfrLink; }

    public int getConditionId()                     { return conditionid; }
    public void setConditionId(int conditionid)     { this.conditionid = conditionid; }

    public String getCondition()                    { return condition; }
    public void setCondition(String condition)      { this.condition = condition; }

    public String getPackage()                      { return packages == null ? "" : packages; }
    public void setPackage(String packages)         { this.packages = packages; }

    public String getPackageInclude()               { return packagesInclude; }
    public void setPackageInclude(String include)   { this.packagesInclude = include; }

    public String getComments()                     { return comments; }
    public void setComments(String comments)        { this.comments = comments; }

    public int getBannerMask()                      { return bannerMask; }
    public void setBannerMask(int bannerMask)       { this.bannerMask = bannerMask; }

    public int getWarrantyMask()                    { return warrantyMask; }
    public void setWarrantyMask(int warrantyMask)   { this.warrantyMask = warrantyMask; }

    public double getWarrantyPrice1()               { return warranty1; }
    public void setWarrantyPrice1(double warranty1) { this.warranty1 = warranty1; }

    public double getWarrantyPrice2()               { return warranty2; }
    public void setWarrantyPrice2(double warranty2) { this.warranty2 = warranty2; }

    public double getWarrantyPrice3()               { return warranty3; }
    public void setWarrantyPrice3(double warranty3) { this.warranty3 = warranty3; }

    public String getPromoCode()                    { return promoCode; }
    public void setPromoCode(String promoCode)      { this.promoCode = promoCode; }

    // second items (relative)
    public double getRelativePrice()                { return relativePrice; }
    public void setRelativePrice(double price)      { this.relativePrice = price; }

    public boolean getRelativeStock()               { return relativeStock; }
    public void setRelativeStock(boolean stock)     { this.relativeStock = stock; }

    public int getRelativeQuantity()                { return relativeQuantity; }
    public void setRelativeQuantity(int quantity)   { this.relativeQuantity = quantity; }

// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="setPromotionCode()">

    public void setPromotionCode(Map<String,Double> promocode) {
        this.promocode = promocode;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getPromotionCode()">

    public Map<String,Double> getPromotionCode() {
        return promocode;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getWarrantyDays()">

    public String getWarrantyDays() {

        String warrantyDay = null;
        // manufacture warranty any bit up ?
        if((getWarrantyMask() & MANUFACTURE_WARRANTY) != 0) {

            if((getWarrantyMask() & MANUFACTURE_W30D) != 0) {
                warrantyDay = W30DAYS;
            }
            else if((getWarrantyMask() & MANUFACTURE_W45D) != 0) {
                warrantyDay = W45DAYS;
            }
//          else if((getWarrantyMask() & MANUFACTURE_W90D) != 0) {
//                warrantyDay = W90DAYS;
//            }
            else if((getWarrantyMask() & MANUFACTURE_W180D) != 0) {
                warrantyDay = W180DAYS;
            }
            else if((getWarrantyMask() & MANUFACTURE_W1Y) != 0) {
                warrantyDay = W1YEAR;
            }
            else if((getWarrantyMask() & MANUFACTURE_W3Y) != 0) {
                warrantyDay = W3YEARS;
            }
            else if((getWarrantyMask() & MANUFACTURE_W5Y) != 0) {
                warrantyDay = W5YEARS;
            }
        }

        return warrantyDay == null ? "n/a" : warrantyDay;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getExtendedWarrantyOptions()">

    public String getExtendedWarrantyOptions(double basketWarrantyPrice, boolean isTagOption) {

        if((getWarrantyMask() & EXTENDED_WARRANTY) == 0) return "";

        int pos = 1;
        StringBuilder buff = new StringBuilder(200);

        if((getWarrantyMask() & WARRANTY_30D)  != 0) buff.append(getOption(pos++, W30DAYS,   isTagOption));
        if((getWarrantyMask() & WARRANTY_60D)  != 0) buff.append(getOption(pos++, W60DAYS,   isTagOption));
        if((getWarrantyMask() & WARRANTY_90D)  != 0) buff.append(getOption(pos++, W90DAYS,   isTagOption));
        if((getWarrantyMask() & WARRANTY_180D) != 0) buff.append(getOption(pos++, W180DAYS,  isTagOption));
        if((getWarrantyMask() & WARRANTY_1Y)   != 0) buff.append(getOption(pos++, W1YEAR,    isTagOption));
        if((getWarrantyMask() & WARRANTY_3Y)   != 0) buff.append(getOption(pos++, W3YEARS,   isTagOption));
        if((getWarrantyMask() & WARRANTY_5Y)   != 0) buff.append(getOption(pos++, W5YEARS,   isTagOption));
        if((getWarrantyMask() & WARRANTY_LIFE) != 0) buff.append(getOption(pos,   WLIFETIME, isTagOption));

        if(isTagOption) {
            String repl = String.format("|%.2f'", basketWarrantyPrice);

            return buff.toString().replace(repl, repl + " selected='yes'");
        }
        else {
            return buff.toString();
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getOption()">

    private String getOption(int pos, String desc, boolean isTagOption) {

        if(pos > 3)  return ""; // only 3 line accept

        double wprice = 0d;
	switch(pos) {
	    case 1: wprice = getWarrantyPrice1(); break;
	    case 2: wprice = getWarrantyPrice2(); break;
	    case 3: wprice = getWarrantyPrice3(); break;
	}

        if(wprice == 0d) return "";

        if(isTagOption) {
            return String.format("<option value='%s|%.2f'>+ %s $%,.2f</option>", desc, wprice, desc, wprice);
        }
        else {
            return String.format("<br/> + %s $%(,.2f", desc, wprice);
        }
    }
// </editor-fold>


// <editor-fold defaultstate="collapsed" desc="toString()">

    @Override
    public String toString() {
        return "id=" + itemID;
    }
// </editor-fold>

}
